package com.huanglj.himalaya.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.presenters.PlayerPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.PlayableModel;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.player.advertis.IXmAdsStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.IXmPlayerStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayerException;

import androidx.annotation.Nullable;
import de.hdodenhof.circleimageview.CircleImageView;

@SuppressLint("AppCompatCustomView")
public class RotateImageView extends CircleImageView implements IXmPlayerStatusListener {

    private static final String TAG = "RotateImageView";
    boolean mIsPlay = false;

    float rotateDegress = 0;

    public RotateImageView(Context context) {
        this(context, null);
    }

    public RotateImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotateImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        XmPlayerManager.getInstance(BaseApplication.getContext()).addPlayerStatusListener(this);
        remotePlay();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.rotate(rotateDegress, getWidth() / 2, getHeight() / 2);
        super.onDraw(canvas);
    }


    @Override
    public void onPlayStart() {
        remotePlay();
    }

    private void remotePlay() {
        mIsPlay = true;
        post(new Runnable() {
            @Override
            public void run() {
                rotateDegress += 0.3;
                rotateDegress = rotateDegress <= 360 ? rotateDegress : 0;
                invalidate();
                if (mIsPlay == true) {
                    postDelayed(this,1000/60);
                }
            }
        });
    }

    @Override
    public void onPlayPause() {
        mIsPlay = false;
    }

    @Override
    public void onPlayStop() {
        mIsPlay = false;
    }

    @Override
    public void onSoundPlayComplete() {

    }

    @Override
    public void onSoundPrepared() {

    }

    @Override
    public void onSoundSwitch(PlayableModel playableModel, PlayableModel playableModel1) {

    }

    @Override
    public void onBufferingStart() {

    }

    @Override
    public void onBufferingStop() {

    }

    @Override
    public void onBufferProgress(int i) {

    }

    @Override
    public void onPlayProgress(int i, int i1) {

    }

    @Override
    public boolean onError(XmPlayerException e) {
        return false;
    }
}
